<template>
  <div class="app-container">
    <el-tabs v-model="queryParams.type" @tab-click="getList">
      <el-tab-pane v-for="(i, k) in tabList" :key="k" :label="i.label" :name="i.value" />
    </el-tabs>
    <template v-if="queryParams.type == 'CUSTOMER_STORIES'">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="queryParams.title" placeholder="请输入文章标题" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['system:article:add']">
            新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete()"
            v-hasPermi="['system:article:remove']">
            删除
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
      </el-row>

      <el-table v-loading="loading" :data="articleList" border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="标题" align="center" prop="title" />
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:article:edit']">
              修改
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:article:remove']">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.params.pageNum"
        :limit.sync="queryParams.params.pageSize" @pagination="getList" />
    </template>
    <template v-else>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <Editors v-model="form.content" ref="formedit" :min-height="192" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">保 存</el-button>
        </el-form-item>
      </el-form>
    </template>
    <el-dialog :title="form.id ? '新增文章' : '修改文字'" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false">
      <el-form ref="form2" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="文章内容" prop="content">
          <Editors v-model="form.content" :min-height="192" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listArticle,
  getArticle,
  delArticle,
  addArticle,
  updateArticle,
} from "@/api/system/article";

export default {
  name: "Article",
  data() {
    return {
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      articleList: [], // 文章表格数据
      title: "", // 弹出层标题
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        type: "ABOUT_US", // 当前选中标签页
        params: {
          pageNum: 1,
          pageSize: 10,
        }
      },
      form: {}, // 表单参数
      rules: {
        title: [{ required: true, message: "文章标题不能为空", trigger: "blur" }],
        content: [{ required: true, message: "文章内容不能为空", trigger: "blur" }],
      }, // 表单校验
      tabList: [
        { label: "关于我们", value: "ABOUT_US" },
        { label: "用户协议", value: "USER_AGREEMENT" },
        { label: "隐私协议", value: "PRIVACY_AGREEMENT" },
        { label: "中酒云网云市场用户协议", value: "CLOUD_MARKET_USER_AGREEMENT" },
        { label: "客户案例", value: "CUSTOMER_STORIES" },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询文章列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams)
        .then((res) => {
          if (this.queryParams.type === "CUSTOMER_STORIES") {
            this.articleList = res.rows;
            this.total = res.total;
          } else {
            this.form = res.rows[0];
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        content: null,
        type: null,
        title: null,
      };
      this.resetForm(this.queryParams.type == "CUSTOMER_STORIES" ? "form2" : "form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.params.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.title = ''
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getArticle(id).then((res) => {
        this.form = res.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs[this.queryParams.type == "CUSTOMER_STORIES" ? "form2" : "form"].validate(
        (valid) => {
          if (valid) {
            if (this.form.id) {
              updateArticle(this.form).then((res) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              this.form.type = this.queryParams.type;
              addArticle(this.form).then((res) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        }
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row ? [row.id] : this.ids;
      this.$modal
        .confirm('确认删除文章编号为"' + ids + '"的数据项？')
        .then(() => {
          delArticle(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => { });
    },
  },
};
</script>
